

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cai Shibo">
  <meta name="keywords" content="cc">
  
    <meta name="description" content="该文章记录对 树 相关题目的分析理解。">
<meta property="og:type" content="article">
<meta property="og:title" content="算法---树">
<meta property="og:url" content="https://hahsx_xd.gitee.io/2022/03/18/%E7%AE%97%E6%B3%95---%E6%A0%91/index.html">
<meta property="og:site_name" content="🍊CAI SHIBO🥬">
<meta property="og:description" content="该文章记录对 树 相关题目的分析理解。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-18T00:53:00.000Z">
<meta property="article:modified_time" content="2022-11-29T07:14:46.511Z">
<meta property="article:author" content="Cai Shibo">
<meta property="article:tag" content="树">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>算法---树 - 🍊CAI SHIBO🥬</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"hahsx_xd.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":".|","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":"#"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":5},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":true,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="🍊CAI SHIBO🥬" type="application/atom+xml">
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>^_^ 🥬 CC</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="算法---树">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      Cai Shibo
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-03-18 08:53" pubdate>
        2022年3月18日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      14k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      68 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">算法---树</h1>
            
              <p class="note note-info">
                
                  最后更新：23 天前
                
              </p>
            
            <div class="markdown-body">
              <p><strong>该文章记录对 树 相关题目的分析理解。</strong></p>
<span id="more"></span>

<hr>
<h4 id="JZ28-对称二叉树-E"><a href="#JZ28-对称二叉树-E" class="headerlink" title="JZ28 对称二叉树 E"></a>JZ28 对称二叉树 E</h4><p>描述：给定一棵二叉树，判断其是否是自身的镜像（即是否对称） </p>
<p>解：检测节点是否为null，null则为对称；检测对称节点是否都拥有左子节点对应且val相等，如果不满足则不对称；检测当前树整体是否对称。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isSymmetrical</span><span class="hljs-params">(TreeNode pRoot)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (pRoot == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> check(pRoot.left, pRoot.right);<br>    &#125;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">check</span><span class="hljs-params">(TreeNode l, TreeNode r)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (l == <span class="hljs-keyword">null</span> &amp;&amp; r == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ((l == <span class="hljs-keyword">null</span> &amp;&amp; r != <span class="hljs-keyword">null</span>) || (l != <span class="hljs-keyword">null</span> &amp;&amp; r == <span class="hljs-keyword">null</span>) || l.val != r.val) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> check(l.left, r.right) &amp;&amp; check(l.right, r.left);<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ68-二叉搜索树的最近公共祖先-E"><a href="#JZ68-二叉搜索树的最近公共祖先-E" class="headerlink" title="JZ68 二叉搜索树的最近公共祖先 E"></a>JZ68 二叉搜索树的最近公共祖先 E</h4><p>思路：借助二叉搜索树的特性：<strong>左子节点 &lt; 根节点 &lt; 右子节点</strong>。从根结点开始寻找，如果遇到与p、q其中一个值相同 或 p、q两个值在当前节点的左右两边，那么最近的公共祖先即为当前节点。如果p、q小于当前节点值，则去递归至当前节点的左节点；反之至右节点。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">lowestCommonAncestor</span> <span class="hljs-params">(TreeNode root, <span class="hljs-keyword">int</span> p, <span class="hljs-keyword">int</span> q)</span> </span>&#123;<br>        <span class="hljs-comment">// write code here</span><br>        <span class="hljs-keyword">if</span>(root.val == p || root.val == q || root.val &gt; p &amp;&amp; root.val &lt; q || root.val &lt; p &amp;&amp; root.val &gt; q) &#123;<br>            <span class="hljs-keyword">return</span> root.val;<br>        &#125;<br>        <span class="hljs-keyword">if</span>(root.val &gt; p &amp;&amp; root.val &gt; q) &#123;<br>            <span class="hljs-keyword">return</span> lowestCommonAncestor(root.left, p, q);<br>        &#125;<br>        <span class="hljs-keyword">return</span> lowestCommonAncestor(root.right, q, p);<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<hr>
<h4 id="JZ86-在二叉树中找到两个节点的最近公共祖先-M"><a href="#JZ86-在二叉树中找到两个节点的最近公共祖先-M" class="headerlink" title="JZ86 在二叉树中找到两个节点的最近公共祖先 M"></a>JZ86 在二叉树中找到两个节点的最近公共祖先 M</h4><p>描述：给定一棵二叉树(保证非空)以及这棵树上的两个节点对应的val值 o1 和 o2，请找到 o1 和 o2 的最近公共祖先节点。</p>
<p>解：不同于搜索二叉树，其没有大小的限制。同样使用递归算法，  </p>
<ol>
<li>如果节点为null，返回null；</li>
<li>如果节点的值等于o1或o2，返回当前节点；</li>
<li>递归节点的左右子树，左子树返回给left，右子树返回给right；</li>
<li>left为null，说明o1、o2均在右子树，返回right；反之返回left；如果两者不为null，说明o1、o2在当前节点左右两边，返回当前节点root。</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">lowestCommonAncestor</span> <span class="hljs-params">(TreeNode root, <span class="hljs-keyword">int</span> o1, <span class="hljs-keyword">int</span> o2)</span> </span>&#123;<br>        <span class="hljs-comment">// write code here</span><br>        <span class="hljs-keyword">return</span> find(root, o1, o2).val;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> TreeNode <span class="hljs-title">find</span><span class="hljs-params">(TreeNode root, <span class="hljs-keyword">int</span> o1, <span class="hljs-keyword">int</span> o2)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (root == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (root.val == o1 || root.val == o2) &#123;<br>            <span class="hljs-keyword">return</span> root;<br>        &#125;<br>        TreeNode left = find(root.left, o1, o2);<br>        TreeNode right = find(root.right, o1, o2);<br>        <span class="hljs-keyword">if</span> (left == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> right;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (right == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> left;<br>        &#125;<br>        <span class="hljs-keyword">return</span> root;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<hr>
<h4 id="JZ55-二叉树深度-E"><a href="#JZ55-二叉树深度-E" class="headerlink" title="JZ55 二叉树深度 E"></a>JZ55 二叉树深度 E</h4><p>描述：求一颗二叉树的深度。</p>
<ol>
<li><strong>bfs广度优先搜索</strong><br> 解：将同一层的节点全部入队，记录该层节点数量（因为后期会将该层节点的子节点入队，队列中数量会变化），for循环该层节点依次出队，出队节点的所有子节点依次入队（即为下一层所有节点），深度++。 <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.LinkedList;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">TreeDepth</span><span class="hljs-params">(TreeNode root)</span> </span>&#123;<br>      <span class="hljs-keyword">if</span>(root == <span class="hljs-keyword">null</span>) &#123;<br>          <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>      &#125;<br>      LinkedList&lt;TreeNode&gt; list = <span class="hljs-keyword">new</span> LinkedList&lt;&gt;();<br>      list.add(root);<br>      <span class="hljs-keyword">int</span> deep = <span class="hljs-number">0</span>;<br>      <span class="hljs-keyword">while</span>(!list.isEmpty()) &#123;<br>          <span class="hljs-keyword">int</span> n = list.size();<br>          <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; ++i)&#123;<br>              TreeNode t = list.poll();<br>              <span class="hljs-keyword">if</span>(t.left != <span class="hljs-keyword">null</span>) &#123;<br>                  list.add(t.left);<br>              &#125;<br>              <span class="hljs-keyword">if</span>(t.right != <span class="hljs-keyword">null</span>) &#123;<br>                  list.add(t.right);<br>              &#125;<br>          &#125;<br>          deep++;<br>      &#125;<br>      <span class="hljs-keyword">return</span> deep;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
</li>
<li><strong>dfs深度优先搜索</strong><br>解：二叉树的深度 = 根节点 + 左右子节点的深度最大值<br>每一个子节点都可以看成根结点。因此可以划分为子问题，递归解决。  </li>
</ol>
  <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.LinkedList;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">TreeDepth</span><span class="hljs-params">(TreeNode root)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span>(root == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> Math.max(TreeDepth(root.left), TreeDepth(root.right)) + <span class="hljs-number">1</span>;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ79-判断是否为平衡二叉树-E"><a href="#JZ79-判断是否为平衡二叉树-E" class="headerlink" title="JZ79 判断是否为平衡二叉树 E"></a>JZ79 判断是否为平衡二叉树 E</h4><p>描述：判断二叉树是否为平衡二叉树  </p>
<ol>
<li><strong>回溯</strong><br>解：对于父节点，只需确定两个子节点深度之差小于一；对于作为子节点，只需向自己的父节点传递自己的深度（或已不是平衡二叉树的标识）。  </li>
</ol>
<p><strong>主体思路与dfs求树深度相同，但要注意当子树不是二叉树，整体树的深度差可能小于1，因此需要一个标识。</strong></p>
  <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">IsBalanced_Solution</span><span class="hljs-params">(TreeNode root)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (deep(root) != -<span class="hljs-number">1</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">deep</span><span class="hljs-params">(TreeNode tree)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (tree == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>        &#125;<br>        <span class="hljs-keyword">int</span> left = deep(tree.left);<br>        <span class="hljs-keyword">int</span> right = deep(tree.right);<br><br>        <span class="hljs-comment">//左右子树深度差小于1且左右子树均仍为平衡叉树，返回当前节点的深度（两树深度差+1）。</span><br>        <span class="hljs-keyword">if</span> (Math.abs(left - right) &lt;= <span class="hljs-number">1</span> &amp;&amp; left != -<span class="hljs-number">1</span> &amp;&amp; right != -<span class="hljs-number">1</span>) &#123;<br>            <span class="hljs-keyword">return</span> Math.max(left, right) + <span class="hljs-number">1</span>;<br>        &#125;<br>        <span class="hljs-comment">//否则传递已不是二叉树的标识</span><br>        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<ol start="2">
<li>dfs 递归:<br>解：遍历每个节点，dfs得每个节点的深度，判断其是否平衡。</li>
</ol>
  <figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">IsBalanced_Solution</span><span class="hljs-params">(TreeNode root)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span>(root == <span class="hljs-keyword">null</span>)<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        <span class="hljs-keyword">return</span> IsBalanced_Solution(root.left) &amp;&amp; IsBalanced_Solution(root.right) &amp;&amp; Math.abs(deep(root.left) - deep(root.right)) &lt; <span class="hljs-number">2</span>;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">deep</span><span class="hljs-params">(TreeNode tree)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (tree == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> Math.max(deep(tree.left), deep(tree.right)) + <span class="hljs-number">1</span>;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ82-二叉树中和为某一路径的值（一）E"><a href="#JZ82-二叉树中和为某一路径的值（一）E" class="headerlink" title="JZ82 二叉树中和为某一路径的值（一）E"></a>JZ82 二叉树中和为某一路径的值（一）E</h4><p>描述：给定一个二叉树root和一个值 sum ，判断是否有从根节点到叶子节点的节点值之和等于 sum 的路径。</p>
<p><strong>递归：</strong>当某节点的左右子节点不为null，说明该节点不是叶子节点，sum = sum - 该节点val，前往其左右节点；反之，则说明该节点为叶子节点，此时判断该节点的val是否等于sum。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">hasPathSum</span> <span class="hljs-params">(TreeNode root, <span class="hljs-keyword">int</span> sum)</span> </span>&#123;<br>        <span class="hljs-comment">// write code here</span><br>        <span class="hljs-keyword">if</span> (root == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (root.left == <span class="hljs-keyword">null</span> &amp;&amp; root.right == <span class="hljs-keyword">null</span> &amp;&amp; root.val == sum) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        &#125;<br>        <span class="hljs-comment">//关键：或，左右子树均要判断</span><br>        <span class="hljs-keyword">return</span> hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ34-二叉树中和为某一路径的值（二）M"><a href="#JZ34-二叉树中和为某一路径的值（二）M" class="headerlink" title="JZ34 二叉树中和为某一路径的值（二）M"></a>JZ34 二叉树中和为某一路径的值（二）M</h4><p>描述：输入一颗二叉树的根节点root和一个整数expectNumber，找出二叉树中结点值的和为expectNumber的所有路径。<em><strong>与（一）不同点：需要找出所有路径</strong></em>。  </p>
<p>解：整体类似于题一。重点：<strong>全局变量记录；减当前节点值的操作要放在判断前；最后回溯的时候要把对应的节点去掉</strong></p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.ArrayList;<br><span class="hljs-keyword">import</span> java.util.LinkedList;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    ArrayList&lt;ArrayList&lt;Integer&gt;&gt; arr = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();<br>    LinkedList&lt;Integer&gt; list = <span class="hljs-keyword">new</span> LinkedList&lt;&gt;();<br>    <span class="hljs-keyword">public</span> ArrayList&lt;ArrayList&lt;Integer&gt;&gt; FindPath(TreeNode root,<span class="hljs-keyword">int</span> expectNumber) &#123;<br>        <span class="hljs-keyword">if</span>(root == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> arr;<br>        &#125;<br>        <span class="hljs-comment">//必须放上面，否则会缺少</span><br>        expectNumber -= root.val;<br>        list.add(root.val);<br>        <span class="hljs-keyword">if</span>(root.left == <span class="hljs-keyword">null</span> &amp;&amp; root.right == <span class="hljs-keyword">null</span> &amp;&amp; expectNumber == <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-comment">//转换的方法</span><br>            arr.add(<span class="hljs-keyword">new</span> ArrayList&lt;Integer&gt;(list));<br>        &#125;<br>        FindPath(root.left, expectNumber);<br>        FindPath(root.right, expectNumber);<br>        <span class="hljs-comment">//回溯到上一层，去掉这层加入的节点</span><br>        list.removeLast();<br>        <span class="hljs-keyword">return</span> arr;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ84-二叉树中和为某一路径的值（三）M"><a href="#JZ84-二叉树中和为某一路径的值（三）M" class="headerlink" title="JZ84 二叉树中和为某一路径的值（三）M"></a>JZ84 二叉树中和为某一路径的值（三）M</h4><p>描述：给定一个二叉树root和一个整数值 sum ，求该树有多少路径的的节点值之和等于 sum 。<em><strong>与（二）不同点：不需要从根结点开始、叶子节点结束，只需返回路径数量即可</strong></em>。  </p>
<p>解：从根结点开始，dfs当sum==0时路径数量+1；但此时不结束，继续记录，防止出现后续节点、节点和为0的情况。此外每个子节点都重新看做根结点进行相同算法。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> k = <span class="hljs-number">0</span>;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">FindPath</span> <span class="hljs-params">(TreeNode root, <span class="hljs-keyword">int</span> sum)</span> </span>&#123;<br>        <span class="hljs-comment">// write code here</span><br>        <span class="hljs-comment">//为了防止出现空指针</span><br>        <span class="hljs-keyword">if</span>(root == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> k;<br>        &#125;<br>        deep(root, sum);<br>        FindPath(root.left, sum);<br>        FindPath(root.right, sum);<br>        <span class="hljs-keyword">return</span> k;<br>    &#125;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">deep</span> <span class="hljs-params">(TreeNode tree, <span class="hljs-keyword">int</span> num)</span> </span>&#123;<br>        <span class="hljs-comment">//为了防止出现空指针</span><br>        <span class="hljs-keyword">if</span>(tree == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span>;<br>        &#125;<br>        num -= tree.val;<br>        <span class="hljs-keyword">if</span>(num == <span class="hljs-number">0</span>) &#123;<br>            k ++;<br>            <span class="hljs-comment">//不return是为了防止出现后面的节点、节点和出现0的情况</span><br>        &#125;<br>        deep(tree.left, num);<br>        deep(tree.right, num);<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<hr>
<h4 id="JZ54-二叉搜索树的第k个节点-M"><a href="#JZ54-二叉搜索树的第k个节点-M" class="headerlink" title="JZ54 二叉搜索树的第k个节点 M"></a>JZ54 二叉搜索树的第k个节点 M</h4><p>描述：给定一棵结点数为n 二叉搜索树，请找出其中的第 k 小的TreeNode结点值。</p>
<p>解：dfs的衍生。二叉搜索树特性—<strong>左节点 &lt; 根结点 &lt; 右节点</strong>。故只需dfs遍历所有节点，同时记录当前节点的值大小排序为多少，当等于k时，直接返回即可。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>, result = -<span class="hljs-number">1</span>;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">KthNode</span> <span class="hljs-params">(TreeNode pRoot, <span class="hljs-keyword">int</span> k)</span> </span>&#123;<br>        <span class="hljs-comment">// write code here</span><br>        <span class="hljs-comment">//防止k = 0的情况</span><br>        <span class="hljs-keyword">if</span>(pRoot == <span class="hljs-keyword">null</span> || k == <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>        &#125;<br>        getInt(pRoot, k);<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">getInt</span><span class="hljs-params">(TreeNode tree , <span class="hljs-keyword">int</span> k)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (tree.left != <span class="hljs-keyword">null</span>) &#123;<br>            getInt(tree.left, k);<br>        &#125;<br>        count ++;<br>        <span class="hljs-keyword">if</span> (count == k) &#123;<br>            result = tree.val;<br>            <span class="hljs-keyword">return</span>;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (tree.right != <span class="hljs-keyword">null</span>) &#123;<br>            getInt(tree.right, k);<br>        &#125;<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ7-重建二叉树-M"><a href="#JZ7-重建二叉树-M" class="headerlink" title="JZ7 重建二叉树 M"></a>JZ7 重建二叉树 M</h4><p>描述：给定节点数为 n 的二叉树的前序遍历和中序遍历结果，请重建出该二叉树并返回它的头结点。</p>
<p>解：前序遍历规律为—根结点、左节点、右节点，中序遍历规律为—左节点、根结点、右节点。<br>易知：前序遍历的第一个值在中序遍历中将其分为左右子树，递归直至完全遍历，即可重建二叉树。<br><strong>数组拷贝函数Arrays.copyOfRange(被拷贝数组，开始位置【包含】，结束位置【不包含】)</strong></p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> TreeNode <span class="hljs-title">reConstructBinaryTree</span><span class="hljs-params">(<span class="hljs-keyword">int</span> [] pre,<span class="hljs-keyword">int</span> [] vin)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (pre.length &lt; <span class="hljs-number">1</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>        &#125;<br>        TreeNode tree = <span class="hljs-keyword">new</span> TreeNode(pre[<span class="hljs-number">0</span>]);<br>        <span class="hljs-comment">//i &lt; pre.length，前序遍历的第一个为根结点，去判断中序遍历的左右子树</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; vin.length; ++i) &#123;<br>            <span class="hljs-keyword">if</span> (pre[<span class="hljs-number">0</span>] == vin[i]) &#123;<br>            <span class="hljs-comment">//数组拷贝函数Arrays.copyOfRange()</span><br>            <span class="hljs-comment">//注意：左、右节点pre的开始和结束位置是有要求的，防止递归重复执行TreeNode tree = new TreeNode(pre[0]);</span><br>            tree.left = reConstructBinaryTree<br>       (Arrays.copyOfRange(pre, <span class="hljs-number">1</span>, i + <span class="hljs-number">1</span>), Arrays.copyOfRange(vin, <span class="hljs-number">0</span>, i));<br>            tree.right = reConstructBinaryTree<br>       (Arrays.copyOfRange(pre, i + <span class="hljs-number">1</span>, pre.length), Arrays.copyOfRange(vin, i + <span class="hljs-number">1</span>, vin.length));<br>            <span class="hljs-comment">//递归后当前for循环必须结束，否则会去找下一个根结点，造成重复</span><br>            <span class="hljs-keyword">break</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> tree;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ26-树的子结构-M"><a href="#JZ26-树的子结构-M" class="headerlink" title="JZ26 树的子结构 M"></a>JZ26 树的子结构 M</h4><p>描述：输入两棵二叉树A，B，判断B是不是A的子结构。（我们约定空树不是任意一个树的子结构）</p>
<p>解：dfs或bfs遍历A树，每个节点都用递归去检查<strong>是否为当前节点为根结点的树的子结构。</strong><br>由于题目规定空树不算子结构，因此需要一个函数来判断是否为子结构。（因为第一次遇到数组长度为0，表示空树，返回false；而之后遇到数组长度为0，表示树已经遍历完且符合要求，返回true）</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">HasSubtree</span><span class="hljs-params">(TreeNode root1,TreeNode root2)</span> </span>&#123;<br>        <span class="hljs-comment">/*bfs遍历</span><br><span class="hljs-comment">        if (root1 == null || root2 == null) &#123;</span><br><span class="hljs-comment">            return false;</span><br><span class="hljs-comment">        &#125;</span><br><span class="hljs-comment">        LinkedList&lt;TreeNode&gt; list = new LinkedList&lt;TreeNode&gt;();</span><br><span class="hljs-comment">        list.add(root1);</span><br><span class="hljs-comment">        while (list.size() &gt; 0) &#123;</span><br><span class="hljs-comment">            int n = list.size();</span><br><span class="hljs-comment">            for (int i = 0; i &lt; n; ++i) &#123;</span><br><span class="hljs-comment">                TreeNode tree = list.poll();</span><br><span class="hljs-comment">                if (isSame(tree, root2)) &#123;</span><br><span class="hljs-comment">                    return true;</span><br><span class="hljs-comment">                &#125;</span><br><span class="hljs-comment">                if (tree.left != null) &#123;</span><br><span class="hljs-comment">                    list.add(tree.left);</span><br><span class="hljs-comment">                &#125;</span><br><span class="hljs-comment">                if (tree.right != null) &#123;</span><br><span class="hljs-comment">                    list.add(tree.right);</span><br><span class="hljs-comment">                &#125;</span><br><span class="hljs-comment">            &#125;</span><br><span class="hljs-comment">        &#125;</span><br><span class="hljs-comment">        return false;*/</span><br>        <br>        <span class="hljs-comment">//dfs遍历</span><br>        <span class="hljs-keyword">if</span> (root1 == <span class="hljs-keyword">null</span> || root2 == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> isSame(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);<br>    &#125;<br>    <br>    <span class="hljs-comment">//必须要借助函数，因为root2本身为空不算作子结构。</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isSame</span><span class="hljs-params">(TreeNode t1, TreeNode t2)</span> </span>&#123;<br>        <span class="hljs-comment">//如果此时root2为空，说明其前面的所有节点都已与A子树的节点相同</span><br>        <span class="hljs-keyword">if</span> (t2 == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (t1 == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> t1.val == t2.val &amp;&amp; isSame(t1.left, t2.left) &amp;&amp; isSame(t1.right, t2.right);<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ33-二叉搜索树的后序遍历序列-M"><a href="#JZ33-二叉搜索树的后序遍历序列-M" class="headerlink" title="JZ33 二叉搜索树的后序遍历序列 M"></a>JZ33 二叉搜索树的后序遍历序列 M</h4><p>描述：输入一个整数数组，判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回 true ,否则返回 false 。假设输入的数组的任意两个数字都互不相同。</p>
<p>解：二叉搜索树特性—<strong>左节点 &lt; 根结点 &lt; 右节点</strong>；同时后续遍历，数组末尾一定为根结点。因此顺序遍历数组，找到左右子树分界处，<strong>判断左子树节点是否均小于根结点，右子树节点是否均大于根节点</strong>。再递归检查左右子树是否满足要求。<br>由于题目规定空树不是二叉树，因此需要借助一个函数来判断。（因为第一次遇到数组长度为0，表示空树，返回false；而之后遇到数组长度为0，表示树已经遍历完且符合要求，返回true）</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">VerifySquenceOfBST</span><span class="hljs-params">(<span class="hljs-keyword">int</span> [] sequence)</span> </span>&#123;<br>        <span class="hljs-keyword">int</span> n = sequence.length;<br>        <span class="hljs-keyword">if</span> (n == <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> check(sequence);<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">check</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] sequence)</span> </span>&#123;<br>        <span class="hljs-keyword">int</span> n = sequence.length;<br>        <span class="hljs-keyword">if</span> (n &lt;= <span class="hljs-number">2</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>        &#125;<br>        <span class="hljs-keyword">boolean</span> pd = <span class="hljs-keyword">false</span>;<br>        <span class="hljs-keyword">int</span> index = n - <span class="hljs-number">2</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n - <span class="hljs-number">1</span>; ++i) &#123;<br>            <span class="hljs-keyword">if</span> (sequence[n - <span class="hljs-number">1</span>] &lt; sequence[i] &amp;&amp; ! pd) &#123;<br>                pd = <span class="hljs-keyword">true</span>;<br>                index = i;<br>            &#125;<br>            <span class="hljs-keyword">if</span> (pd &amp;&amp; sequence[n - <span class="hljs-number">1</span>] &gt; sequence[i]) &#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> check(Arrays.copyOfRange(sequence, <span class="hljs-number">0</span>, index)) &amp;&amp; <br>            check(Arrays.copyOfRange(sequence, index, n - <span class="hljs-number">1</span>));<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
<hr>
<h4 id="JZ36-二叉搜索树与双向链表-M"><a href="#JZ36-二叉搜索树与双向链表-M" class="headerlink" title="JZ36 二叉搜索树与双向链表 M"></a>JZ36 二叉搜索树与双向链表 M</h4><p>描述：输入一棵二叉搜索树，将该二叉搜索树转换成一个排序的双向链表。<br>注意：1.要求不能创建任何新的结点，只能调整树中结点指针的指向。当转化完成以后，树中节点的左指针需要指向前驱，树中节点的右指针需要指向后继；2.返回链表中的第一个节点的指针；3.函数返回的TreeNode，有左右指针，其实可以看成一个双向链表的数据结构；4.你不用输出双向链表，程序会根据你的返回值自动打印输出。</p>
<p>解：二叉搜索树 左节点 &lt; 根结点 &lt; 右节点。<br>从左-中-右遍历树，使用pre记录前驱节点，使用root记录最左节点；除了头节点情况外，pre为当前节点的前驱节点，当前节点为pre的后继节点，将他们双向链接，pre = 当前节点。递归即可。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    TreeNode pre = <span class="hljs-keyword">null</span>;<br>    TreeNode root = <span class="hljs-keyword">null</span>;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> TreeNode <span class="hljs-title">Convert</span><span class="hljs-params">(TreeNode pRootOfTree)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (pRootOfTree == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>        &#125;<br>        Convert(pRootOfTree.left);<br>        <span class="hljs-keyword">if</span> (root == <span class="hljs-keyword">null</span>) &#123;<br>            root = pRootOfTree;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (pre != <span class="hljs-keyword">null</span>) &#123;<br>            pRootOfTree.left = pre;<br>            pre.right = pRootOfTree;<br>        &#125;<br>        pre = pRootOfTree;<br>        Convert(pRootOfTree.right);<br>        <span class="hljs-keyword">return</span> root;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<hr>
<h4 id="JZ8-二叉树的下一个结点-M"><a href="#JZ8-二叉树的下一个结点-M" class="headerlink" title="JZ8 二叉树的下一个结点 M"></a>JZ8 二叉树的下一个结点 M</h4><p>描述：给定一个二叉树其中的一个结点，请找出中序遍历顺序的下一个结点并且返回。注意，树中的结点不仅包含左右子结点，同时包含指向父结点的next指针。下图为一棵有9个节点的二叉树。树中从父节点指向子节点的指针用实线表示，从子节点指向父节点的用虚线表示。</p>
<p>解：暴力求解，先使用输入的头节点的next去寻找树的根结点；得到中序遍历的排序，找到头节点在中序遍历中的位置，并输出。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> TreeLinkNode <span class="hljs-title">GetNext</span><span class="hljs-params">(TreeLinkNode pNode)</span> </span>&#123;<br>        <span class="hljs-comment">//得到根结点</span><br>        TreeLinkNode tree = pNode;<br>        <span class="hljs-keyword">while</span> (tree.next != <span class="hljs-keyword">null</span>) &#123;<br>            tree = tree.next;<br>        &#125;<br>        <span class="hljs-comment">//中序遍历树，得到数组</span><br>        getMid(tree);<br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; arr.size(); ++i) &#123;<br>            <span class="hljs-keyword">if</span> (arr.get(i).val == pNode.val) &#123;<br>                <span class="hljs-keyword">return</span> i == arr.size() - <span class="hljs-number">1</span> ? <span class="hljs-keyword">null</span> : arr.get(i + <span class="hljs-number">1</span>);<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br>    <br>    ArrayList&lt;TreeLinkNode&gt; arr = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">getMid</span><span class="hljs-params">(TreeLinkNode tree)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (tree.left != <span class="hljs-keyword">null</span>) &#123;<br>            getMid(tree.left);<br>        &#125;<br>        arr.add(tree);<br>        <span class="hljs-keyword">if</span> (tree.right != <span class="hljs-keyword">null</span>) &#123;<br>            getMid(tree.right);<br>        &#125;<br>        <span class="hljs-keyword">return</span>;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p><strong>优化：</strong>暴力求解空间复杂度O(n)。为了使空间复杂度为O(1)，模拟中序遍历查找。  </p>
<ol>
<li>根据中序遍历的特点，先去检查当前节点有无右子树；  </li>
<li>如有：进一步检查其右子树有无左节点；如无：检查其是否有无父节点且为父节点的右子树；</li>
<li>如无父节点返回null，当前是根结点且无右子树，是中序遍历中的最后一个元素，返回null；如有父节点、是父节点的左子树，那中序遍历中父节点一定是后一个，返回父节点；如有父节点且是父节点的右子树，检查父节点是否有父节点且对父节点来说是左子树。</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> TreeLinkNode <span class="hljs-title">GetNext</span><span class="hljs-params">(TreeLinkNode pNode)</span> </span>&#123;<br>        <span class="hljs-comment">//判断是否有右子树</span><br>        <span class="hljs-keyword">if</span> (pNode.right != <span class="hljs-keyword">null</span>) &#123;<br>            pNode = pNode.right;<br>            <span class="hljs-comment">//查找右子树的最左节点</span><br>            <span class="hljs-keyword">while</span> (pNode.left != <span class="hljs-keyword">null</span>) &#123;<br>                pNode = pNode.left;<br>            &#125;<br>            <span class="hljs-keyword">return</span> pNode;<br>        &#125;<br>        <span class="hljs-comment">//无右子树情况</span><br>        <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-comment">//检查有无父节点，无则返回null</span><br>            <span class="hljs-keyword">if</span> (pNode.next != <span class="hljs-keyword">null</span>) &#123;<br>                <span class="hljs-comment">//有父节点，判断是否为父节点的左子树</span><br>                <span class="hljs-keyword">if</span> (pNode.next.left == pNode) &#123;<br>                    <span class="hljs-comment">//是左子树，返回父节点</span><br>                    <span class="hljs-keyword">return</span> pNode.next;<br>                &#125;<br>                <span class="hljs-comment">//是右子树</span><br>                <span class="hljs-keyword">else</span> &#123;<br>                    pNode = pNode.next;<br>                    <span class="hljs-comment">//检查树的部分是否为左子树，如果是，返回作为左子树时的父节点</span><br>                    <span class="hljs-keyword">while</span> (pNode.next != <span class="hljs-keyword">null</span> &amp;&amp; pNode.next.left != pNode) &#123;<br>                        pNode = pNode.next;<br>                    &#125;<br>                    <span class="hljs-comment">//遍历到根结点都没找到，就返回null</span><br>                    <span class="hljs-keyword">return</span> pNode.next;<br>                &#125;<br>            &#125;<br>            <span class="hljs-comment">//无父节点</span><br>            <span class="hljs-keyword">else</span> &#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<hr>
<h4 id="LC-面试题0406-后继者-M"><a href="#LC-面试题0406-后继者-M" class="headerlink" title="LC 面试题0406 后继者 M"></a>LC 面试题0406 后继者 M</h4><p>描述：设计一个算法，找出二叉搜索树中指定节点的“下一个”节点（也即中序后继）。如果指定节点没有对应的“下一个”节点，则返回null。</p>
<p>解：中序遍历树节点，记录当前节点和其前一节点，判断前一节点是否与目标节点相同。若相同则返回当前节点，反之继续遍历。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> TreeNode <span class="hljs-title">inorderSuccessor</span><span class="hljs-params">(TreeNode root, TreeNode p)</span> </span>&#123;<br>        Stack&lt;TreeNode&gt; stack = <span class="hljs-keyword">new</span> Stack&lt;&gt;();<br>        TreeNode pre = <span class="hljs-keyword">null</span>;<br>        <span class="hljs-keyword">while</span> (root != <span class="hljs-keyword">null</span> || !stack.isEmpty()) &#123;<br>            <span class="hljs-keyword">while</span> (root != <span class="hljs-keyword">null</span>) &#123;<br>                stack.push(root);<br>                root = root.left;<br>            &#125;<br>            root = stack.pop();<br>            <span class="hljs-keyword">if</span> (pre == p) &#123;<br>                <span class="hljs-keyword">return</span> root;<br>            &#125;<br>            pre = root;<br>            root = root.right;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>

<p><strong>优化：</strong>根据 BST 特性，节点的 val 按照中序遍历递增。  </p>
<ul>
<li><p>当前节点为 null 时都没有找到匹配的，则说明不存在返回 null；</p>
</li>
<li><p>当前节点值小于等于目标节点值时，表示其在目标节点的左边，即在中序遍历中在目标节点的前面，一定不是，因此往右子树查找；</p>
</li>
<li><p>当前节点值大于目标节点值时，表示其在目标节点的右侧，即在中序遍历中在目标节点后面；此时存在两种情况：</p>
<ul>
<li>当前节点就是目标节点后一个节点，即题目目标节点</li>
<li>题目目标节点在当前节点的子树中</li>
</ul>
</li>
<li><p>因此需要新建一个对象继续查询，如果返回为 null，说明当前节点即为目标，反之，新的值为目标。</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> TreeNode <span class="hljs-title">inorderSuccessor</span><span class="hljs-params">(TreeNode root, TreeNode p)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (root == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (root.val &lt;= p.val) &#123;<br>            <span class="hljs-keyword">return</span> inorderSuccessor(root.right, p);<br>        &#125;<br>        TreeNode ans = inorderSuccessor(root.left, p);<br>        <span class="hljs-keyword">return</span> ans == <span class="hljs-keyword">null</span> ? root : ans;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>


<hr>
<h4 id="LC22-括号生成-M"><a href="#LC22-括号生成-M" class="headerlink" title="LC22 括号生成 M"></a>LC22 括号生成 M</h4><p>描述：数字 n 代表生成括号的对数，请你设计一个函数，用于能够生成所有可能的并且 有效的 括号组合。</p>
<p>解：dfs 算法。在条件允许的情况下，先不断加 “(” ，然后加 “)” 直到左右括号都是使用完。<br>条件即为左边的括号使用量必须要大于等于右边，不符合条件则需要进行剪枝。</p>
<figure class="highlight java"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;String&gt; <span class="hljs-title">generateParenthesis</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>&#123;<br>        dfs(n, n, <span class="hljs-string">&quot;&quot;</span>);<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>    <span class="hljs-keyword">private</span> List&lt;String&gt; result = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">dfs</span><span class="hljs-params">(<span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> right, String temp)</span> </span>&#123;<br>        <span class="hljs-keyword">if</span> (left == <span class="hljs-number">0</span> &amp;&amp; right == <span class="hljs-number">0</span>) &#123;<br>            result.add(temp);<br>            <span class="hljs-keyword">return</span>;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (left &gt; right) &#123;<br>            <span class="hljs-keyword">return</span>;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (left &gt; <span class="hljs-number">0</span>) &#123;<br>            dfs(left - <span class="hljs-number">1</span>, right, temp + <span class="hljs-string">&quot;(&quot;</span>);<br>        &#125;<br>        <span class="hljs-keyword">if</span> (right &gt; <span class="hljs-number">0</span>) &#123;<br>            dfs(left, right - <span class="hljs-number">1</span>, temp + <span class="hljs-string">&quot;)&quot;</span>);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></div></td></tr></table></figure>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E7%AE%97%E6%B3%95/">算法</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E6%A0%91/">树</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章未经允许，严禁转载！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/03/21/Java%E5%9F%BA%E7%A1%80/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Java基础</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/03/17/SSM--D5/">
                        <span class="hidden-mobile">SSM--D5</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     CC <i class="iconfont icon-love"></i> XX
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    
      <script  src="/js/img-lazyload.js" ></script>
    
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
